local skel = fk.CreateSkill{
  name = "est__zhuxing",
  tags = { Skill.Compulsory, Skill.Switch }
}
--local U = require "packages/utility/utility"
Fk:loadTranslationTable{
  ["est__zhuxing"] = "佇行",
  [":est__zhuxing"] = "转换技，锁定技，阳：当你对自己使用牌后或弃牌阶段结束时，你将牌堆顶X张牌置于武将牌上(X为“笔山”选择的角色数且至少为1)；"..
  "阴：当你对其他角色使用牌后，你获得武将牌上的一张牌（无牌同样转换），当你使用以此法获得的牌时，你摸两张牌。",
  ["#est__zhuxing-yin"] = "佇行：阴，选择一张武将牌上的牌获得，使用后会有额外效果",
  ["@@est__zhuxing-inhand"] = "佇行",
}
---@type TrigSkelSpec<UseCardFunc>
local zhuxing_yang = {
  anim_type = "switch",
  can_trigger = function (self, event, target, player, data)
    return player:hasSkill(skel.name) and player == target and player:getSwitchSkillState(skel.name, false) == fk.SwitchYang and data:hasTarget(player)
  end,
  on_use = function (self, event, target, player, data)
    local room = player.room
    local num = math.max(#player:getTableMark("@@est__bishan-turn"), 1)
    local cid = room:getNCards(num)
    if #cid > 0 then
      player:addToPile(skel.name, cid, true, skel.name, player)
    end
  end
}
---@type TrigSkelSpec<PhaseFunc>
local zhuxing_yang_ = {
  anim_type = "control",
  can_trigger = function (self, event, target, player, data)
    return player:hasSkill(skel.name) and player == target and player.phase == Player.Discard
    and player:getSwitchSkillState(skel.name, false) == fk.SwitchYang
  end,
  on_use = zhuxing_yang.on_use
}
---@type TrigSkelSpec<UseCardFunc>
local zhuxing_yin = {
  anim_type = "switch",
  can_trigger = function (self, event, target, player, data)
    return player:hasSkill(skel.name) and player == target and player:getSwitchSkillState(skel.name, false) == fk.SwitchYin
      and table.find(player.room:getOtherPlayers(player, true, true), function (p)
        return data:hasTarget(p)
      end)
  end,
  on_use = function (self, event, target, player, data)
    local cids = player:getPile(skel.name)
    if #cids > 0 then
      local room = player.room
      local cid = room:askToCards(player, {
        min_num = 1,
        max_num = 1,
        skill_name = skel.name,
        prompt = "#est__zhuxing-yin",
        expand_pile = cids,
        pattern = tostring(Exppattern{ id = cids })
      })
      if #cid == 1 then
        room:obtainCard(player, cid, true, fk.ReasonJustMove, player, skel.name)
        if not player.dead and table.contains(player:getCardIds("h"), cid[1]) then
          room:setCardMark(Fk:getCardById(cid[1]), "@@est__zhuxing-inhand", 1)
        end
      end
    end
  end
}
---@type TrigSkelSpec<UseCardFunc>
local zhuxing_delay = {
  anim_type = "drawcard",
  is_delay_effect = true,
  can_trigger = function (self, event, target, player, data)
    return player:hasSkill(skel.name, true) and player == target and data.card and data.card:getMark("est__zhuxing_delay") > 0
  end,
  on_use = function (self, event, target, player, data)
    player.room:setCardMark(data.card, "est__zhuxing_delay", 0)
    player:drawCards(2, skel.name)
  end
}
---@type TrigSkelSpec<UseCardFunc>
local zhuxing_refresh = {
  is_delay_effect = true,
  can_refresh = function (self, event, target, player, data)
    return player == target and data.card and data.card:getMark("@@est__zhuxing-inhand") > 0
  end,
  on_refresh = function (self, event, target, player, data)
    player.room:setCardMark(data.card, "@@est__zhuxing-inhand", 0)
    player.room:setCardMark(data.card, "est__zhuxing_delay", 1)
  end
}
---@type TrigSkelSpec<UseCardFunc>
local zhuxing_refresh_ = {
  is_delay_effect = true,
  can_refresh = function (self, event, target, player, data)
    return player:hasSkill(skel.name, true) and player == target
  end,
  on_refresh = function (self, event, target, player, data)
    for _, id in ipairs(player:getCardIds("h")) do
      local c = Fk:getCardById(id)
      player.room:setCardMark(c, "@@est__zhuxing-inhand", 0)
    end
  end
}
skel:addEffect(fk.CardUseFinished, zhuxing_yang)
skel:addEffect(fk.EventPhaseEnd, zhuxing_yang_)
skel:addEffect(fk.CardUseFinished, zhuxing_yin)
skel:addEffect(fk.CardUsing, zhuxing_delay)
skel:addEffect(fk.PreCardUse, zhuxing_refresh)
--skel:addEffect(fk.TurnEnd, zhuxing_refresh_)
return skel